package projetA;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class BtnAdmin extends JButton implements ActionListener{
	
	private String bt_text;
	private MainClass sup;
	private JLabel imageGauche;
	private JLabel imageCentre;
	private JLabel imageDroite;
	//private JFrame mainFrame;
	JPanel zoneImage = new JPanel();
	
	public BtnAdmin(String _txt , MainClass _sup){
		super(_txt);
		this.bt_text = _txt;
		this.sup = _sup;		
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(this.bt_text == "IMAGE"){
			JFileChooser file_chooser = new JFileChooser();
			file_chooser.setDialogType(JFileChooser.OPEN_DIALOG);
			int returnVal = file_chooser.showSaveDialog(file_chooser);
			if (returnVal == JFileChooser.APPROVE_OPTION){
				try {
					System.out.println(file_chooser.getSelectedFile().getAbsolutePath());
					zoneImage.removeAll();
					zoneImage.setVisible(true);
					zoneImage.repaint();
					sup.tbImage = ImageSplit.split(file_chooser.getSelectedFile().getAbsolutePath());						
						ImageIcon icon = new ImageIcon(this.scaleImage((500/3), 400, sup.tbImage[0]));
						//icon = this.scaleImage((500/3), 400, sup.tbImage[0]);
						imageGauche  = new JLabel(icon, JLabel.CENTER);
						imageGauche.setLayout(null);
						icon = new ImageIcon(this.scaleImage((500/3), 400, sup.tbImage[1]));
						imageCentre  = new JLabel(icon, JLabel.CENTER);
						imageCentre.setLayout(null);
						icon = new ImageIcon(this.scaleImage((500/3), 400, sup.tbImage[2]));
						imageDroite  = new JLabel(icon, JLabel.CENTER);
						imageDroite.setLayout(null);
						zoneImage.add(imageGauche);
						zoneImage.add(imageCentre);	
						zoneImage.add(imageDroite);
					sup.getContentPane().add(BorderLayout.CENTER,zoneImage);
					createNewInstanceThreads();
					sup.btnGo.setEnabled(true);
					sup.setVisible(true);
					sup.repaint();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else if(this.bt_text == "GO"){
			if(!sup.testThreadsVivant()){	
				sup.setPauseThread(false);
				sup.threadGauche.start();
				sup.threadCentre.start();
				sup.threadDroite.start();
				sup.btnStop.setEnabled(true);
			}
			else if(sup.testThreadsVivant()){
				sup.setPauseThread(false);
				sup.btnGo.setEnabled(false);
				sup.btnStop.setEnabled(true);
			}		
		}else if(this.bt_text == "STOP"){
			if(sup.testThreadsVivant()){					
				sup.setPauseThread(true);
				sup.btnGo.setEnabled(true);
				sup.btnStop.setEnabled(false);
			}else if(!sup.testThreadsVivant()){
				JOptionPane.showMessageDialog(null, "Aucune animation en cours" , "Impossible" , -1);
			}
		}
		
	}
	
	
	public static BufferedImage scaleImage(int w, int h, BufferedImage img) throws Exception {
        BufferedImage bi;
        bi = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(img, 0, 0, w, h, null);
        g2d.dispose();
        return bi;
    }
	
	public void createNewInstanceThreads(){
		sup.threadGauche = new ThreadImageGauche(this);
		sup.threadCentre = new ThreadImageCentre(this);
		sup.threadDroite = new ThreadImageDroite(this);
	}
	
	public MainClass getMain(){
		return this.sup;
	}
}
